package com.yidu.action;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.model.LockerBox;
import com.yidu.model.LockerLease;
import com.yidu.model.LockerUser;
import com.yidu.service.BoxService;
import com.yidu.service.LeaseService;
import com.yidu.util.TimeUtil;
import com.yidu.util.Tools;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.plaf.synth.SynthSeparatorUI;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author roots
 * @since 2018-10-20萨杜
 * 
 */
@Controller
@RequestMapping("/lease")
public class LeaseController {
	@Resource
	LeaseService service;
	
	@Resource
	BoxService boxservice;

	/**
	 * 添加
	 * @param lockerLease
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("insertve")
	@ResponseBody
	public LockerLease insertSelective(LockerLease lockerLease,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String uuid=UUID.randomUUID().toString().replaceAll("-", "");
		//租聘ID
		lockerLease.setLesId(uuid);
		//柜子ID
		lockerLease.setBoxId(lockerLease.getBoxId());
		System.out.println("柜子的id:"+lockerLease.getBoxId());
		LockerBox lockerBoxs=boxservice.selectboxid(lockerLease.getBoxId());
		
		//用户ID
		HttpSession session=request.getSession();
		//取session
		LockerUser lockerUser=(LockerUser) session.getAttribute("user");
		//判断是否为空
		if(lockerUser!=null) {
			//添加用户
			lockerLease.setUserId(lockerUser.getUserId());
		}
		
		
		//设置cld
		Calendar calendar = Calendar.getInstance();
		//获取当前时间
		calendar.setTime(new Date());
		//当前时间的日增加
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		
		//存入时间
		lockerLease.setLesOpen(new Date());
		//规定时间
		lockerLease.setLesDeadline(calendar.getTime());
		
		//判断是否逾期
		if(lockerLease.getLesOpen().equals(calendar.getTime())) {
			System.out.println("逾期");
		}
		
		//逾期
		lockerLease.setLesState("0");
		//是否缴纳逾期金
		lockerLease.setLesPay("0");
		
		//物品信息
		System.out.println("物品信息:"+lockerLease.getLesThing());
		lockerLease.setLesThing(lockerLease.getLesThing());
		
		//创建随机数
		Random random=new Random();
		int i=random.nextInt(99999999);
		//转string
		String ran=String.valueOf(i);
		
		//取件码
		lockerLease.setLesCode(ran);
		
		//租金		
		System.out.println("lockerBoxs.getBoxRent()="+lockerBoxs.getBoxRent());
		lockerLease.setLesMoneys(String.valueOf(lockerBoxs.getBoxRent()));
		
		int ic=0;
		//判断用户是否为空
		if(lockerLease.getUserId()==null) {
			System.out.println("null");
		}else {
			System.out.println("true");
			//调用增加的方法
			ic=service.insertSelective(lockerLease);
		}
		//修改柜子的状态
		if(ic>0) {
			//调用修改的方法
			int boxupdate=boxservice.updateId(lockerLease.getBoxId());
			if(boxupdate>0) {
				System.out.println("成功");
			}else {
				System.out.println("失败");
			}
		}
		return lockerLease;
	}
	
	@RequestMapping("/selectcode")
	@ResponseBody
	public LockerLease selectcode(LockerLease lockerLease){
		LockerLease code = service.selectcode(lockerLease);
		if(code == null) {
			code = new LockerLease();
		}
		return code;
	}
	
	@RequestMapping("/endup")
	@ResponseBody
	public int endup(LockerLease lockerLease) {
		lockerLease.setLesEnd(new Date());
		return service.endup(lockerLease);
	}
	
	@RequestMapping("/usercome")
	@ResponseBody
	public List<LockerLease> usercome(String userId){
		List<LockerLease> list=service.usercome(userId);
		List<LockerLease> lists=new ArrayList<>();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			LockerLease lockerLease = (LockerLease) iterator.next();
			lockerLease.setOpenDate(TimeUtil.dateToString(lockerLease.getLesOpen(),"yyyy-MM-dd HH:mm:ss	"));
			lists.add(lockerLease);
		}
		return lists;
	}
}